package com.airn.siege.lion.lib.widget.number.picker.d

import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.NumberPicker
import java.lang.reflect.Field
import java.util.*

/**
 * Author: Airn
 * Create On: 2022-11-30
 * E-mail: 1275218537@qq.com
 */
class ChargeNumberPicker : NumberPicker {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun addView(child: View) {
        super.addView(child)
        updateView(child)
    }

    override fun addView(
        child: View, index: Int, params: ViewGroup.LayoutParams
    ) {
        super.addView(child, index, params)
        updateView(child)
    }

    override fun addView(child: View, params: ViewGroup.LayoutParams) {
        super.addView(child, params)
        updateView(child)
    }

    private fun updateView(view: View) {
        if (view is EditText) { //这里修改字体的属性
            view.setTextColor(Color.parseColor("#000000"))
            view.textSize = 18f
        }
    }

    //动态调整
    fun updateColor(colorStr: String) {
        val count = childCount
        for (i in 0 until count) {
            val child = getChildAt(i)
            if (child is EditText) {
                try {
                    val selectorWheelPaintField: Field = Objects.requireNonNull(javaClass.superclass).getDeclaredField("mSelectorWheelPaint")
                    selectorWheelPaintField.isAccessible = true
                    (selectorWheelPaintField.get(this) as Paint).color = Color.parseColor(colorStr)
                    child.setTextColor(Color.parseColor(colorStr))
                    this.invalidate()
                } catch (e: NoSuchFieldException) {

                } catch (e: IllegalAccessException) {
                    e.printStackTrace()
                }
            }
        }
    }

    fun setNumberPickerDividerColor(numberPicker: NumberPicker, lineColorStr: String = "#0091ea") {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { //Android 10
            numberPicker.showDividers = NumberPicker.SHOW_DIVIDER_BEGINNING
            numberPicker.dividerDrawable = ColorDrawable(Color.parseColor(lineColorStr))
            numberPicker.selectionDividerHeight = 4
            return
        }

        val pickerFields = NumberPicker::class.java.declaredFields
        for (pf in pickerFields) {
            if (pf.name == "mSelectorIndices") {
                pf.isAccessible = true
                try {
                    pf.set(numberPicker, ColorDrawable(Color.parseColor(lineColorStr)))
                } catch (e: IllegalArgumentException) {
                    Log.e("ChargeNumberPicker", "setNumberPickerDividerColor-IllegalArgumentException = " + e.message)
                } catch (e: Resources.NotFoundException) {
                    Log.e("ChargeNumberPicker", "setNumberPickerDividerColor-NotFoundException = " + e.message)
                } catch (e: IllegalAccessException) {
                    Log.e("ChargeNumberPicker", "setNumberPickerDividerColor-IllegalAccessException = " + e.message)
                }
                break
            }
        }
    }

}